package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.dto.KillParamDto;
import cn.itsource.ymcc.exception.GlobleException;
import cn.itsource.ymcc.fallback.KillFallback;
import cn.itsource.ymcc.service.IKillCourseService;
import cn.itsource.ymcc.domain.KillCourse;
import cn.itsource.ymcc.query.KillCourseQuery;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.result.PageList;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;



    //执行秒杀方法
    @PostMapping("/kill")
    @SentinelResource(value = "kill",fallback = "killFallback",blockHandler = "limitFlowFallback")
    public JSONResult kill(@RequestBody  KillParamDto dto){
        try {
            return JSONResult.success(killCourseService.kill(dto));
        }catch (GlobleException e){
            return JSONResult.error(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
    public JSONResult killFallback(@RequestBody  KillParamDto dto,Throwable ex){
        ex.printStackTrace();
        return JSONResult.error("服务器繁忙，请稍后重试");
    }
    // 限流与阻塞处理 : 参数要和 被降级的方法参数一样
    public JSONResult limitFlowFallback(@RequestBody  KillParamDto dto, BlockException ex) {
        ex.printStackTrace();
        return JSONResult.error("请求数量太多，请重试");
    }




    //获取单个秒杀课程
    @GetMapping("/online/one/{id}")
    public JSONResult getOnlineKillCourseById(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.getOnlineKillCourseById(id));
    }
    //获取已经发布的课程列表
    @GetMapping("/online/all")
    public JSONResult getOnlineAllKillCourse(){
        return JSONResult.success(killCourseService.getOnlineAllKillCourse());
    }


    //添加秒杀课程
    @PostMapping("/add")
    public JSONResult add(@RequestBody KillCourse killCourse){
        killCourseService.add(killCourse);
        return JSONResult.success();
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查看所有
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult json(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
